#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

typedef pair<int, int> PII;

const int N = 1e6 + 10;
int n, m, idx, dist[N];
int h[N], e[N], ne[N], w[N];
bool st[N];

void add(int a, int b, int c)
{
    e[idx] = b;
    ne[idx] = h[a];
    w[idx] = c;
    h[a] = idx ++;
}

void dijkstra()
{
    memset(dist, 0x3f, sizeof dist);
    
    
    priority_queue<PII, vector<PII>, greater<PII>> heap;
    heap.push({0, 1});
    for(int i = 0; i <= n; i++) dist[i]=1000000;
    dist[1] = 0;
    while(heap.size())
    {
        auto t = heap.top();
        heap.pop();
        
        int distance = t.first, ver = t.second;
        if(st[ver])  continue;//当前的点是冗余备份
        st[ver] = true;
        
        for(int i = h[ver]; i != -1; i = ne[i])
        {
            int j = e[i];
            if(dist[j] > distance + w[i])
            {
                dist[j] = distance + w[i];
                heap.push({dist[j], j});
            }
        }
    }
    for(int i = 0; i <= n; i++) 
    {
        if(dist[i] == 1000000)
        {
            dist[i] = -1;
        }
        
    }
    // if(dist[n] == 0x3f3f3f3f)  return -1;
    for(int i = 1; i <= n; i++)
    {
        cout << dist[i] << " ";
    }
    // return dist[n];
}

int main()
{
    cin >> n >> m;
    memset(h, -1, sizeof h);
    
    while(m --)
    {
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c);
    }
    
    dijkstra();
    return 0;
}
